package managedBeans;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperManager;
import net.sf.jasperreports.engine.JasperPrint;




@WebServlet(description = "Servlet Chamada de Relatorio", urlPatterns = { "/chamarServlet" })
public class RelatorioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   public RelatorioServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processaRequisicao(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
	
	private void processaRequisicao(HttpServletRequest req,
			HttpServletResponse resp) {

			String driver = "com.mysql.jdbc.Driver";
			String server = "jdbc:mysql://localhost:3307/clinica_odontologica";
			String user   = "root";
			String pass   = "root";
			
			try {
                                
				Class.forName(driver);
				Connection conn = DriverManager.getConnection(server, user, pass);
				Map parametros = new HashMap();
				String jasperPath = getServletContext().getRealPath("/WEB-INF/relatorio/")+"/";
				String path = getServletContext().getRealPath("/");
                                System.out.println("ENTROU AKI NO RELATORIO");
                                System.out.println(jasperPath);
                                JasperPrint impressao = JasperFillManager.fillReport(jasperPath+"relatorioDoutor.jasper", parametros, conn);
				JasperManager.printReportToPdfFile(impressao, path+"/Relatorio.pdf");
				resp.sendRedirect("Relatorio.pdf");
				
				
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JRException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		
	

}
